#include "songdownloadhttp.h"
#include "QFile"
#include "log/EZQLOG.h"

using namespace EZQHTTPNS;

SongDownLoadHttp::SongDownLoadHttp()
{
//    setNeedProgress(true);
    _song = 0;
    _willStart = false;
//    setStopOnUnCite(true);
    setCancelOnUnCite(true);
    EZQHttp::setNeedProgress(true);
}
SongDownLoadHttp::~SongDownLoadHttp()
{
    EZDebug << "~SongDownLoadHttp";
}
void SongDownLoadHttp::Release()
{
    retainCount--;
    int count = retainCount;
//    retainCount--;
    if(_cancelOnUnCite==true && count==1 && _status==HttpStatus_runing){
//        _mutex.lock();
        cancel();
//        _mutex.unlock();
    }

    if (count==0) {
        delete this;
    }

}
void SongDownLoadHttp::setDownLoadPath(const QString &downLoadPath)
{
    QFile::remove(downLoadPath);
    _downLoadPath = downLoadPath;
}
void SongDownLoadHttp::setNeedProgress(bool /*isNeed*/)
{
    return;
}
SongModel *SongDownLoadHttp::song() const
{
    return _song;
}

void SongDownLoadHttp::setSong(SongModel *song)
{
    if(song){
        connect(song,SIGNAL(sigSongUrlChange(QString&,int)),this,SLOT(OnSongGetUrl(QString&,int)));
        song->Retain();
    }
    if(_song){
        disconnect(_song,SIGNAL(sigSongUrlChange(QString&,int)),this,SLOT(OnSongGetUrl(QString&,int)));
        _song->Release();
    }
    _song = song;
}

void SongDownLoadHttp::startSync()
{
    return;
}

void SongDownLoadHttp::startAsync()
{
    QString key = bestQualityKey();
    if(key.isEmpty() || QString::compare(key,songHash_normal)==0 ){
        setStatus(HttpStatus_error);
        emit finish();
    }else{
//        SongModel::Quality quality = _song->getUrlWith(key);
        int getType;
        QString url = _song->getUrlWith(key,&getType);
        if(getType >=0 ){
            //get url first
            if(url.isEmpty()){
                _mutex.lock();
                _willStart = true;
                Retain();
                setStatus(HttpStatus_start);
                setStatus(HttpStatus_runing);
                _receiveByte.clear();
                _receiveByteLen = 0;
                _mutex.unlock();

            }else{
                setUrl(url);
                startRequest();
            }
        }else{
            setStatus(HttpStatus_error);
            emit finish();
        }
    }

}
QString SongDownLoadHttp::bestQualityKey()
{
//    QString filePath = _song->getUrlWith(songHash_localPath);
    QString localkey(songHash_localPath);
    SongModel::Quality localQuality = _song->qualityWithKey(localkey);
    if(localQuality.url.isEmpty()==false){ // local
        //local music
        return songHash_localPath;
    }
    QString key;
    SongModel::Quality quality;
    key = QString(songHash_320);
    quality = _song->qualityWithKey(key);
    if(quality.hash.isEmpty()){
        key = QString(songHash_normal);
        quality = _song->qualityWithKey(key);
    }
    return key;
}

void SongDownLoadHttp::OnSongGetUrl(QString &key,int error)
{
    bool isWillRelease = false;
    _mutex.lock();
    if(_willStart){
        _willStart = false;
        SongModel::Quality quality;
        if(error==0){
            quality = _song->qualityWithKey(key);
            if(quality.url.isEmpty()){
                qDebug() << "error get song url finis bug error url is null";
                setStatus(HttpStatus_error);
                emit finish();
            }else{
//                EZDebug << quality.url;
                setUrl(quality.url);
                startRequest();
            }
        }else{
            qDebug() << "error get song url finis bug error";
            setStatus(HttpStatus_error);
            emit finish();
        }

        isWillRelease = true;
//        Release();
    }
    _mutex.unlock();
    if(isWillRelease==true){
        Release();
    }
}

void SongDownLoadHttp::cancel()
{
    bool isRelease = false;
    _mutex.lock();
    if(_willStart){
        _willStart = false;
//        Release();
        isRelease = true;
        setStatus(HttpStatus_cancel);
    }
    _mutex.unlock();
    EZQHttp::cancel();
    if(isRelease){
        Release();
    }

}

bool SongDownLoadHttp::cancelOnUnCite() const
{
    return _cancelOnUnCite;
}

void SongDownLoadHttp::setCancelOnUnCite(bool cancelOnUnCite)
{
    _cancelOnUnCite = cancelOnUnCite;
}
